import { ref } from 'vue'
import CacheManager from '@/utils/cache'
import { useMessage } from '@/composables/useMessage'

export function useCache() {
  const { show: showMessage } = useMessage()
  const loading = ref(false)

  /**
   * 清除菜单和路由缓存
   */
  const clearMenuCache = async () => {
    try {
      loading.value = true
      CacheManager.clearMenuCache()
      showMessage('菜单和路由缓存已清除', 'success')
    } catch (error) {
      showMessage('清除菜单缓存失败', 'error')
    } finally {
      loading.value = false
    }
  }

  /**
   * 重新加载菜单和路由
   */
  const reloadMenus = async () => {
    try {
      loading.value = true
      await CacheManager.reloadMenus()
      showMessage('菜单和路由已重新加载', 'success')
    } catch (error) {
      showMessage('重新加载菜单失败', 'error')
    } finally {
      loading.value = false
    }
  }

  /**
   * 清除字典缓存
   */
  const clearDictCache = () => {
    try {
      CacheManager.clearDictCache()
      showMessage('字典缓存已清除', 'success')
    } catch (error) {
      showMessage('清除字典缓存失败', 'error')
    }
  }

  /**
   * 清除所有系统缓存
   */
  const clearSystemCache = () => {
    try {
      CacheManager.clearSystemCache()
      showMessage('系统缓存已清除', 'success')
    } catch (error) {
      showMessage('清除系统缓存失败', 'error')
    }
  }

  /**
   * 获取缓存状态
   */
  const getCacheStatus = () => {
    return CacheManager.getCacheStatus()
  }

  return {
    loading,
    clearMenuCache,
    reloadMenus,
    clearDictCache,
    clearSystemCache,
    getCacheStatus
  }
} 